<?php

namespace app\admin\middleware;

use think\facade\Request;
// use think\facade\Session;
use think\facade\Cache;
use app\common\Error;
use think\facade\View;

class CheckAdminAuth
{
    public function handle($request, \Closure $next)
    {
        // 访问服务器控制
        $host = Request::host();
        if ($host == 'sybyn.top') {
            return json(['code' => Error::NOT_LOGGED_CODE, 'message' => Error::PLEASE_USE_HT_LIUYIYINGSHI_CN_DOMAIN_NAME_ACCESS]);
        }

        $url = Request::controller() . '/' . Request::action();

        $requestUrl = strtolower($url);
        $notAuthRequestUrl = [
            'finance/settlementcallback',
            'tiktok/refundCallback', // by huangxiaoshan add 2023-07-26 抖音退款回调配置.
            'mediaassets/dycallback',
            'tiktok/newrefundcallback',
            'finance/kwaiordersettlement',
            'export/financedataexport',
        ];

        // 连接redis句柄
        $redis = Cache::store('redis')->handler();
        $redis->select(3);

        if (!in_array($requestUrl, $notAuthRequestUrl)) {

            $header = Request::header();
            if (!empty($header['token'])) {
                $token = $header['token'];
            } else if (!empty(Request::param()['token'])) {
                $token = Request::param()['token'];
            } else {
                $token = '';
            }

            if (empty($token)) {
                return redirect(url('admin/login/index'));
                // return json(['code' => Error::NOT_LOGGED_CODE, 'message' => Error::PLEASE_LOGIN_FIRST]);
            }

            $userInfo = $redis->get($token);
            $userInfo = json_decode($userInfo, true);
            if (empty($userInfo) || empty($userInfo['user_id'])) {
                return redirect(url('admin/login/index'));
                // return json(['code' => Error::NOT_LOGGED_CODE, 'message' => Error::PLEASE_LOGIN_FIRST]);
            }
            $userId = $userInfo['user_id'];
            $ruleId = $userInfo['rule_id'];

            if (empty($ruleId) || empty($userId)) {
                return redirect(url('admin/login/index'));
                // return json(['code' => Error::NOT_LOGGED_CODE, 'message' => Error::PLEASE_LOGIN_FIRST]);
            }

            $redis = Cache::store('redis')->handler();
            $key = config('redis_key.admin.admin_menu_data');

            $authUrl = $redis->get($key . $ruleId);
            if (!$authUrl) {
                return redirect(url('admin/login/index'));
                // return json(['code' => Error::NOT_LOGGED_CODE, 'message' => Error::PLEASE_LOGIN_FIRST]);
            }

            $authArrayUrl = json_decode($authUrl, true);

            $requestUrl = 'admin/' . Request::controller() . '/' . Request::action();
            $url = strtolower($requestUrl);

            // if (!in_array($url, $authArrayUrl)) {
            //     return json(['code' => Error::NOT_REQUEST_AUTH_CODE, 'message' => Error::NOT_REQUEST_AUTH_MESSAGE]);
            // }
        }

        return $next($request);
    }
}